{% comment %} toc(str, class, list_number) {% endcomment %}
{% capture toc %}
  {% assign first_level = nil %}
  {% assign str_stack = str | split: '<h0>' %}
  {% assign level_stack = '0' | split: '' %}
  {% assign title_stack = '' | split: '' | push: nil %}
  {% assign id_stack = '' | split: '' | push: nil %}
  {% assign num_stack = '0' | split: '' %}

  {% assign num_array = '0' | split: '' %}

  {% assign first = true %}
  {% for i in (1..100) %}
    {% if str_stack.size == 0 %}
      {% break %}
    {% endif %}
    {% assign str = str_stack | last %}
    {% assign level = level_stack | last %}
    {% assign title = title_stack | last %}
    {% assign id = id_stack | last %}
    {% assign num = num_stack | last %}

    {% assign str_stack = str_stack | pop %}
    {% assign level_stack = level_stack | pop %}
    {% assign title_stack = title_stack | pop %}
    {% assign id_stack = id_stack | pop %}
    {% assign num_stack = num_stack | pop %}

    {% if first_level %}
      {% assign deep = num_array.size | minus: 1 | minus: level %}

      {% for i in (1..deep) %}
        </li></ol>
      {% endfor %}

      {% unless first %}
        {% if deep < 0 %}
          {% for i in (deep..-1) %}
            <ol class="{{ class }}-child">
          {% endfor %}
        {% else %}
          </li>
        {% endif %}
    {% endunless %}

      {% assign first = false %}
    {% endif %}

    {% assign num_array = num_array | slice: 0, level %}
    {% assign num_array = num_array | push: num %}

    {% if title and num_array.size != 0 %}
      <li class="{{ class }}-item {{ class }}-level-{{ level }}">
        <a class="{{ class }}-link" href="#{{ id }}">
          {% if list_number %}
            {% assign tmp = num_array | slice: first_level, 7 %}
            {% capture number %}{% for i in tmp %}.{{i}}{% endfor %}{% endcapture %}
            {% assign number = number | strip | remove_first: '.' %}
            <span class="{{ class }}-number">{{ number }}</span>
          {% endif %}
          <span class="{{ class }}-text">{{ title }}</span>
        </a>
    {% endif %}

    {% assign level = level | plus: 1 %}

    {% if level > 6 %}
      {% continue %}
    {% endif %}

    {% assign tag_begin = '<h' | append: level %}
    {% assign tag_end = '</h' | append: level | append: '>'%}

    {% unless str contains tag_end %}
      {% assign str_stack = str_stack | push: str %}
      {% assign level_stack = level_stack | push: level %}
      {% assign title_stack = title_stack | push: nil %}
      {% assign id_stack = id_stack | push: nil %}
      {% assign num_stack = num_stack | push: 0 %}
      {% continue %}
    {% endunless %}

    {% unless first_level %}
      {% assign first_level = level %}
    {% endunless %}

    {% assign tags = str | split: tag_begin | shift | reverse %}

    {% for tag in tags %}
      {% assign tmp = tag | split: tag_end %}
      {% assign str = tmp[1] | strip %}

      {% assign tmp = tmp[0] | strip | split: '>' %}
      {% assign title = tmp | shift | join: '>' | strip_html %}

      {% assign tmp = tmp[0] | strip | split: 'id=' %}
      {% assign id = tmp[1] | strip | replace: '"', '' %}

      {% assign str_stack = str_stack | push: str %}
      {% assign level_stack = level_stack | push: level %}
      {% assign title_stack = title_stack | push: title %}
      {% assign id_stack = id_stack | push: id %}
      {% assign num_stack = num_stack | push: forloop.rindex %}
    {% endfor %}
  {% endfor %}
{% endcapture %}
{% assign toc = toc | normalize_whitespace %}
{% if toc.size != 0 %}
  {% capture toc %}
    <ol class={{ class }}>
      {{ toc }}
    </ol>
  {% endcapture %}
{% endif %}
